<?php
// +----------------------------------------------------------------------
// | Fanwe 多语商城建站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 云淡风轻(97139915@qq.com)
// +----------------------------------------------------------------------

//会员资金
class UserMoneyAction extends CommonAction{
	public function incharge()
	{
    	$user_name = $_REQUEST['user_name'];
    	$user_id = intval(M("User")->where("user_name='".$user_name."'")->getField("id")); 	
    	$payment_list = M("Payment")->where("status=1")->findAll();
    	$this->assign("payment_list",$payment_list);
    	
		//列表过滤器，生成查询Map对象
		$map = $this->_search ("UserIncharge");
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		if($user_name&&$user_name!='')
		{
			$map['user_id'] = $user_id;
		}
		$model = D ("UserIncharge");
		if (! empty ( $model )) {
			$this->_list ( $model, $map );
		}
		$this->display ();
		return;		
	}
	public function resumeIncharge()
	{
	//恢复指定记录

		$model = D ("UserIncharge");
		$pk = $model->getPk ();
		$id = $_GET [$pk];
		$condition = array ($pk => array ('in', $id ) );
		if (false !== $model->resume ( $condition ))
		{
			$incharge_info = $model->getById($id);
			D('User')->updateUserScore($incharge_info['user_id'],$id,'Admin','UserIncharge',$incharge_info['money'],'管理员后台恢复所属充值单',true);
			$model -> where("id=".$id)->setField('update_time',gmtTime());
			$sn = $model->where("id=".$id)->getField('sn');
			$msg = "充值单：".$sn." 确定成功";
			$this->saveLog(1,$id,$msg);
			$this->assign ( "jumpUrl", $this->getReturnUrl () );
			
			$this->success (L('CONFIRM_SUCCESS'));
		} else {
			$sn = $model->where("id=".$id)->getField('sn');
			$msg = "充值单：".$sn." 确定失败";
			$this->saveLog(0,0,$msg);
			$this->error ( L('CONFIRM_FAILED') );
		}
	}
	
	public function forbidIncharge()
	{
		$model = D ("UserIncharge");
		$pk = $model->getPk ();
		$id = $_REQUEST [$pk];
		$condition = array ($pk => array ('in', $id ) );
		$list=$model->forbid ( $condition );
		if ($list!==false)
		{	
			$incharge_info = $model->getById($id);
			D('User')->updateUserScore($incharge_info['user_id'],$id,'Admin','UserIncharge',"-".$incharge_info['money'],'管理员后台取消所属充值单',true);				
			
			$sn = $model->where("id=".$id)->getField('sn');
			$msg = "充值单：".$sn." 取消成功";
			$this->saveLog(1,$id,$msg);
			$this->assign ( "jumpUrl", $this->getReturnUrl () );
			$this->success ( L('CANCEL_SUCCESS') );
		} else {
			$sn = $model->where("id=".$id)->getField('sn');
			$msg = "充值单：".$sn." 取消失败";
			$this->saveLog(0,0,$msg);
			$this->error  (  L('CANCEL_FAILED') );
		}
	}
	public function  foreverdeleteIncharge()
	{
		$name="UserIncharge";
		$model = D ($name);
		if (! empty ( $model )) {
			$pk = $model->getPk ();
			$id = $_REQUEST [$pk];
			$sn = $model->where("id=".$id)->getField('sn');
			if (isset ( $id )) {
				$condition = array ($pk => array ('in', explode ( ',', $id ) ) );
				if (false !== $model->where ( $condition )->delete ()) {
					$msg = "充值单：".$sn." 删除成功";
					$this->saveLog(1,$id,$msg);
					$this->success (L('DEL_SUCCESS'));
				} else {
					
					$msg = "充值单：".$sn." 删除失败";
					$this->saveLog(0,0,$msg);
					$this->error (L('DEL_FAILED'));
				}
			} else {				
				$msg = "充值单删除失败";
				$this->saveLog(0,0,$msg);
				$this->error ( L('INVALID_OP') );
			}
		}
		$this->forward ();
	}
	
	public function score()
	{
		//列表过滤器，生成查询Map对象
		$map = array ();
		$map['user_name'] = $_REQUEST['user_name'];
		$map['begin_create_time'] = $_REQUEST['begin_create_time'];
		$map['end_create_time'] = $_REQUEST['end_create_time'];
		
		$this->assign ( 'user_name', $map['user_name'] );
		$this->assign ( 'begin_create_time', $map['begin_create_time'] );
		$this->assign ( 'end_create_time', $map['end_create_time'] );
				
		$begin_create_time = ! empty ($map['begin_create_time']) ? localStrToTime($map['begin_create_time']) : 0;
		$end_create_time = ! empty ($map['end_create_time']) ? localStrToTime($map['end_create_time']) : 0;
		
		
		$sum_score = 0;
		
		//积分管理
		$sql = "select a.*, b.user_name from ".C("DB_PREFIX")."user_score_log a".
			   " LEFT OUTER JOIN ".C("DB_PREFIX")."user b ON b.id = a.user_id".	
			   " where a.score <> 0 and b.id is not null ";
		
		$sql_str = "select sum(a.score) as score from ".C("DB_PREFIX")."user_score_log a".
			   " LEFT OUTER JOIN ".C("DB_PREFIX")."user b ON b.id = a.user_id".	
			   " where a.score <> 0 and b.id is not null ";
					
		
		if ((!empty($map['user_name'])) && ($map['user_name'] <>'')){
			$sql .= " and b.user_name like '%".$map['user_name']."%'";
			
			$sql_str .= " and b.user_name like '%".$map['user_name']."%'";
		}

		if ($begin_create_time > 0){
			$sql .= " and a.create_time >= '".$begin_create_time."'";
			
			$sql_str .= " and a.create_time < '".$begin_create_time."'";
		}
		
		if ($end_create_time > 0){
			$sql .= " and a.create_time <= '".$end_create_time."'";
		}		
		
		if ($begin_create_time > 0){
			//dump($sql_str);
			$tmp = M()->query($sql_str);
			$sum_score = $tmp[0]['score'];
		}
		
		$this->assign ( 'sum_score', $sum_score);
		
		
		foreach ( $map as $key => $val ) {
			//dump($key);
			if ((!is_array($val)) && ($val <> '')){
				$parameter .= "$key='" . urlencode ( $val ) . "'&";
			}
		}		
		
		//dump($sql);
        $model = D($this->name);		
        $voList = $this->_Sql_list($model, $sql, $parameter, 'create_time', true);
        
		foreach($voList as $k=>$v)
		{
			$sum_score = $sum_score + $v['score'];
			if($v['score']>=0)
			{
				//$list[$k]['dec_score'] = '';
				$voList[$k]['inc_score'] = abs($v['score']);
			}
			else
			{
				//$list[$k]['inc_score'] = '';
				$voList[$k]['dec_score'] = abs($v['score']);
			}
			
			$voList[$k]['sum_score'] = $sum_score;
		}		
		$this->assign ( 'score_log_list', $voList);
				
		$this->display ('UserMoney:user_score_log');
		return;		
	}
}

function showStatusIncharge($status, $id)
{
	switch ($status) {
		case 0 :
			$info = '<a href="javascript:resumeIncharge(' . $id . ')">'.L("CONFIRM").'</a>';
			break;
		case 1 :
			$info = '<a href="javascript:forbidIncharge(' . $id . ')">'.L("CANCEL").'</a>';
			break;
	}
	return $info;
}

function getPaymentName($payment_id)
{

	$payment_info = D("Payment")->getById($payment_id);
	return $payment_info['name'];
}
?>